/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pua.uniandes.servicios;

import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.Singleton;
import javax.ejb.Stateless;
import pua.uniandes.entities.ServidorTransmision;

/**
 *
 * @author jd.velasquez3209
 */
@Singleton
public class ManejoUsuariosBean implements ManejoUsuariosBeanRemote, ManejoUsuariosBeanLocal {

    private ArrayList<String> ipValidas;
    private int cantidadConexionesTotal;
    private int conexionesEntidad;
    private int conexionesOtros;
    private static final int CANTIDAD_MAXIMA_CONEXIONES = 300;
    private ServidorTransmision servidor;

    @Override
    public String request(String ipAddress) {

        System.out.println("camtidad de conexiones: " + cantidadConexionesTotal);


        if (cantidadConexionesTotal <= CANTIDAD_MAXIMA_CONEXIONES) {
            cantidadConexionesTotal++;

            final int i = cantidadConexionesTotal + 3000;
            Thread t = new Thread() {

                public void run() {
                    try {
                        servidor.inicializarTransmisionVideo("localhost", i);
                    } catch (UnknownHostException ex) {
                        Logger.getLogger(ManejoUsuariosBean.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (InterruptedException ex) {
                        Logger.getLogger(ManejoUsuariosBean.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (SocketException ex) {
                        Logger.getLogger(ManejoUsuariosBean.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            };

            t.start();


            return "localhost:" + i;
        } else {

            boolean esEntidad = false;

            for (Iterator<String> it = ipValidas.iterator(); it.hasNext();) {
                String string = it.next();

                if (string.equals(ipAddress)) {

                    esEntidad = true;

                }

            }

            if (esEntidad) {

                desconectarOtro();
                final int i = cantidadConexionesTotal + 3000;
                Thread t = new Thread() {

                    public void run() {
                        try {
                            servidor.inicializarTransmisionVideo("localhost", i);
                        } catch (UnknownHostException ex) {
                            Logger.getLogger(ManejoUsuariosBean.class.getName()).log(Level.SEVERE, null, ex);
                        } catch (InterruptedException ex) {
                            Logger.getLogger(ManejoUsuariosBean.class.getName()).log(Level.SEVERE, null, ex);
                        } catch (SocketException ex) {
                            Logger.getLogger(ManejoUsuariosBean.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                };

                t.start();


                return "localhost:" + i;
            } else {
                return "No se pudo realizar la conexion";
            }
        }



    }

    @PostConstruct
    public void initialize() {


        servidor = new ServidorTransmision();
        cantidadConexionesTotal = 0;
        ipValidas = new ArrayList<String>();

        ipValidas.add("127.0.0.1");

    }

    private void desconectarOtro() {
        throw new UnsupportedOperationException("Not yet implemented");
    }
    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
}
